﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Generico.Coneccion;
using Administracion.ar.com.rofex.ws;
using System.Globalization;

namespace Administracion
{
    public partial class NewsSeleccionNoticias : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            var _FileBrowser1 = new CKFinder.FileBrowser { BasePath = "../ckfinder/" };
            _FileBrowser1.SetupCKEditor(this.txtContenido);

            if (!this.IsPostBack)
            {      

                CargarListas();

                if (lstNoticias.Items.Count >= 1)
                {
                    this.lstNoticias.SelectedIndex = 0;
                    CargarDatos();
                }
                else
                    btnCrearNewsletter.Visible = false;

            }
        }



        private void CargarDatos()
        {

            txtNombre.Text = "NEWS " + DateTime.Now.ToString("dd") + "." + DateTime.Now.ToString("MM") + "." + DateTime.Now.ToString("yyyy");
            txtFecha.Text = DateTime.Now.ToShortDateString();
            txtEstado.Text = "PENDIENTE";
            txtAsunto.Text = "NEWS " + DateTime.Now.ToString("dd") + "." + DateTime.Now.ToString("MM") + "." + DateTime.Now.ToString("yyyy");
            txtContenido.Visible = false;
        }
        
        
        protected void btnAceptar_Click(object sender, EventArgs e)
        {
                     
            var cn = new ClsConeccion();
            cn.Abrir();

            cn.AgregarParametros("Nombre", txtNombre.Text);
            cn.AgregarParametros("Fecha", Convert.ToDateTime(txtFecha.Text));
            cn.AgregarParametros("Estado", "PENDIENTE");
            cn.AgregarParametros("Asunto", CultureInfo.CurrentCulture.TextInfo.ToTitleCase(txtAsunto.Text.ToLower()));
            cn.AgregarParametros("HTML", txtContenido.Text);
            cn.EjecutarSp("sp_ADM_NewNewsLetter");
        
            cn.Cerrar();
            Response.Write("<script> alert('Se ha guardado correctamente');</script>");
          
        }

      

        private void CargarListas()
        {
            var cn = new ClsConeccion();
            cn.Abrir();
            string sql = "Select Id, Titulo FROM Noticias order by Id desc";

            var rd = cn.EjecutarSqlDataReader(sql);
            var dt = new DataTable();
            dt.Load(rd);
            this.lstNoticias.DataSource = dt;
            this.lstNoticias.DataTextField = "Titulo";
            this.lstNoticias.DataValueField = "Id";
            this.lstNoticias.DataBind();

            string sqlArt = "Select id, TituloResumen FROM Articulos order by Id desc";

            var rdArt = cn.EjecutarSqlDataReader(sqlArt);
            var dtArt = new DataTable();
            dtArt.Load(rdArt);
            this.LstArticulos.DataSource = dtArt;
            this.LstArticulos.DataTextField = "TituloResumen";
            this.LstArticulos.DataValueField = "id";
            this.LstArticulos.DataBind();

            this.lstInformes.DataSource = dtArt;
            this.lstInformes.DataTextField = "TituloResumen";
            this.lstInformes.DataValueField = "id";
            this.lstInformes.DataBind();
            
            cn.Cerrar();
        }

        protected void btnCrearNewsletter_Click(object sender, EventArgs e)
        {
            string html = "";

            html += GetHtmlCabeceraNewsletter(DateTime.Now.ToString("dd") + "." + DateTime.Now.ToString("MM") + "." + DateTime.Now.ToString("yyyy"));

            string ids = "";

            foreach (ListItem noticia in lstNoticias.Items)
            {
                if (noticia.Selected)
                {
                    ids += noticia.Value + ", "; 
                }
            }

            var cn = new ClsConeccion();
            cn.Abrir();
            string sql = "Select Id, Titulo, Resumen FROM Noticias where Id in (" + ids.Substring(0, ids.Length - 2) + ")";
            var rd = cn.EjecutarSqlDataReader(sql);
            while (rd.Read())
            {

                html += GetHtmlNoticiaNewsletter(rd["Titulo"].ToString().Trim(), rd["Resumen"].ToString().Trim(), "http://rosariofinanzas.com.ar/Noticia.aspx?id=" + rd["Id"].ToString());

            }
            rd.Close();
            cn.Cerrar();

            string idsArt = "";
            foreach (ListItem articulo in LstArticulos.Items)
            {
                if (articulo.Selected)
                {
                    idsArt += articulo.Value + ", ";
                }
            }

            if (idsArt.Length > 0)
            {
                var cn2 = new ClsConeccion();
                cn2.Abrir();
                string sql2 = "Select id, TituloResumen, Resumen FROM Articulos where id in (" + idsArt.Substring(0, idsArt.Length - 2) + ")";
                var rd2 = cn2.EjecutarSqlDataReader(sql2);
                while (rd2.Read())
                {
                    string resumen = rd2["Resumen"].ToString().Replace("<p>", "");
                    resumen = resumen.Replace("</p>", "");
                    html += GetHtmlArticuloSugeridoNewsletter(rd2["TituloResumen"].ToString(), resumen.Trim(), "http://rosariofinanzas.com.ar/Articulos.aspx?id=" + rd2["id"].ToString());

                }
                rd2.Close();
                cn2.Cerrar();
            }
            
            string idsInf = "";
            foreach (ListItem informe in lstInformes.Items)
            {
                if (informe.Selected)
                {
                    idsInf += informe.Value + ", ";
                }
            }

            if (idsInf.Length > 0)
            {
                var cn3 = new ClsConeccion();
                cn3.Abrir();
                string sql3 = "Select id, TituloResumen, Resumen FROM Articulos where id in (" + idsInf.Substring(0, idsInf.Length - 2) + ")";
                var rd3 = cn3.EjecutarSqlDataReader(sql3);
                while (rd3.Read())
                {

                    string resumen = rd3["Resumen"].ToString().Replace("<p>", "");
                    resumen = resumen.Replace("</p>", "");
                    html += GetHtmlInformeSugeridoNewsletter(rd3["TituloResumen"].ToString(), resumen.Trim(), "http://rosariofinanzas.com.ar/Articulos.aspx?id=" + rd3["id"].ToString());

                }
                rd3.Close();
                cn3.Cerrar();
            }

            html += GetHtmlPiePaginaNewsletter();

            txtContenido.Text = string.Empty;
            txtContenido.Text = html;
            txtContenido.Visible = true;
            btnAgregar.Visible = true;

        }
        


        private string GetHtmlCabeceraNewsletter(string fecha)
        {

            string html = "";

            html += "<div style=\"padding: 30px; height: auto; background-color: rgb(243, 238, 225);\">";
            html += "<div style=\"text-align: center;\">";
            html += "</div>";
            html += "<div style=\"text-align: center; background-color: white;\">";
            html += "<div style=\"width: 100%; height: 20px; background-color: rgb(141, 105, 13);\">";
            html += "</div>";
            html += "<table style=\"width: 96%; height: 120px; text-align: left; margin-top: 10px; margin-right: 2%; margin-left: 2%; border-bottom-color: rgb(141, 105, 13); border-bottom-width: 3px; border-bottom-style: double; background-color: white;\">";
            html += "<tbody>";
            html += "<tr>";
            html += "<td>";
            html += " <img alt=\"Logo Rosario Finanzas\" border=\"0\" src=\"http://rosariofinanzas.com.ar/ckfinder/images/RosarioNews.jpg\" style=\"width: 174px; height: 44px;\" /></td>";
            html += "<td style=\"text-align: right;\">";
            html += "<span id=\"fecha\" style=\"font-family: arial,helvetica,sans-serif; font-size: 24px;\">" + fecha + "</span><br />";
            html += "<span style=\"font-family: arial,helvetica,sans-serif; font-size: 24px;\">news<strong>letter</strong></span><br />";
            html += "<br />";
            html += "<span style=\"color: black; font-family: arial,helvetica,sans-serif; font-size: 14px;\"><a href=\"http://rosariofinanzas.com.ar\" style=\"text-decoration: none;\">rosariofinanzas.com.ar</a></span></td>";
            html += "</tr>";
            html += "<tr>";
            html += "<td>";
            html += "</td>";
            html += "<td style=\"text-align: right;\">";
            html += "</td>";
            html += "</tr>";
            html += "</tbody>";
            html += "</table>";

            return html;
        }

        private string GetHtmlPiePaginaNewsletter()
        {

            string html = "";

            html += "<p>";
            html += " Si no desea seguir recibiendo este correo, por favor, env&iacute;e un mail haciendo click aqu&iacute;&nbsp;<a href=\"mailto:info@rosariofinanzas.com.ar\">info@rosariofinanzas.com.ar</a>&nbsp;con el t&iacute;tulo &quot;REMOVER&quot;</p>";
            html += "<div style=\"padding: 10px; height: 100px; text-align: right; background-color: rgb(141, 105, 13);\">";
            html += "<a href=\"https://www.facebook.com/rosario.finanzas\" target=\"_blank\"><img alt=\"Facebook\" border=\"0\" src=\"http://rosariofinanzas.com.ar/ckfinder/images/face.png\" style=\"border-width: 0px; border-style: solid; width: 23px; height: 22px;\" /></a> <a href=\"https://twitter.com/rosariofinanzas\" target=\"_blank\"> <img alt=\"Twitter\" border=\"0\" src=\"http://rosariofinanzas.com.ar/ckfinder/images/twit.png\" style=\"border-width: 0px; border-style: solid; width: 23px; height: 22px;\" /></a></div>";
            html += "</div>";
            html += "</div>";
            html += "<p>";
            html += "</p>";


            return html;
        }

        private string GetHtmlNoticiaNewsletter(string titulo, string resumen, string url)
        {

            string html = "";

            html += "<table align=\"center\" border=\"0\" cellpadding=\"5\" cellspacing=\"0\" style=\"width: 459px; margin-top: 10px;\">";
            html += "<tbody>";
            html += "<tr>";
            html += "<td>";
            html += "<p id=\"titulo\">";
            html += "<strong>" + titulo + "</strong></p>";
            html += "</td>";
            html += "<td style=\"width: 338px;\">";
            html += "<p id =\"resumen\">" + resumen;
            html += "</p>";
            html += "<p>";
            html += "<a href=\"" + url + "\" target=\"_blank\">" + url +"</a></p>";
            html += "</td>";
            html += "</tr>";
            html += "</tbody>";
            html += "</table>";

            return html;
        }

        private string GetHtmlArticuloSugeridoNewsletter(string titulo, string resumen, string url)
        {

            string html = "";

            html += "<table align=\"center\" border=\"0\" cellpadding=\"5\" cellspacing=\"0\" style=\"width: 459px; margin-top: 10px;\">";
            html += "<tbody>";
            html += "<tr>";
            html += "<td>";
            html += "<p><b>ART&Iacute;CULO&nbsp;</b><br /><strong>SUGERIDO:&nbsp;</strong></p>";
            html += "<p id=\"titulo\">";
            html += "<strong>" + titulo + "</strong></p>";
            html += "</td>";
            html += "<td style=\"width: 338px;\">";
            html += "<p id =\"resumen\">" + resumen;
            html += "</p>";
            html += "<p>";
            html += "<a href=\"" + url + "\" target=\"_blank\">" + url + "</a></p>";
            html += "</td>";
            html += "</tr>";
            html += "</tbody>";
            html += "</table>";

            return html;
        }

        private string GetHtmlInformeSugeridoNewsletter(string titulo, string resumen, string url)
        {

            string html = "";

            html += "<table align=\"center\" border=\"0\" cellpadding=\"5\" cellspacing=\"0\" style=\"width: 459px; margin-top: 10px;\">";
            html += "<tbody>";
            html += "<tr>";
            html += "<td>";
            html += "<p><b>INFORME&nbsp;</b><br /><strong>SUGERIDO:&nbsp;</strong></p>";
            html += "<p id=\"titulo\">";
            html += "<strong>" + titulo + "</strong></p>";
            html += "</td>";
            html += "<td style=\"width: 338px;\">";
            html += "<p id =\"resumen\">" + resumen;
            html += "</p>";
            html += "<p>";
            html += "<a href=\"" + url + "\" target=\"_blank\">" + url + "</a></p>";
            html += "</td>";
            html += "</tr>";
            html += "</tbody>";
            html += "</table>";

            return html;
        }
            
    }
}
